//
//  WFStatusTableViewCell.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/13.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit
import SnapKit
//  首页微博字体大小
let HomeFontSize: CGFloat = 15
class WFStatusTableViewCell: UITableViewCell {

    //记录toolbar顶部约束
    var toolbarTopConstraint:Constraint?
    
    // MARK:  - 持有statusViewModel
    var statusViewModel:WFStatusViewModel? {
        didSet{
            originalView.statusViewModel = statusViewModel
            
            //卸载约束
            toolbarTopConstraint?.uninstall()
            //判断是否有转发微博
            if statusViewModel?.status?.retweeted_status != nil{
                //有转发微博数据
                 retweetView.statusViewModel = statusViewModel
                retweetView.hidden = false
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                  self.toolbarTopConstraint =  make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else{
                retweetView.hidden = true
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
           
            toolBarView.statusViewModel = statusViewModel
            
        }
    }
    
    // MARK:  - 懒加载控件
    //原创微博的视图
    private lazy var originalView:WFStatusOriginalView = WFStatusOriginalView()
    //toolBar视图
    private lazy var toolBarView:WFStatusToolBar = WFStatusToolBar()
    //转发视图
    private lazy var retweetView:WFStatusRetweetView = WFStatusRetweetView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:  - 设置视图
    private func setUpUI(){
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweetView)
        //添加原创视图
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(HomeCellMargin)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            //目前只有这一个视图,所以就只添加这一个 即可
           
        }
        //添加retweetview
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        //添加toolbar视图
        toolBarView.snp_makeConstraints { (make) -> Void in
            self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
           
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBarView)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }

}
